Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    // 中心点纬度、经度
    latitude: "",
    longitude: "",
    scrollTop:0,
    // 标记点 当前位置
    markers: [],    
    // 圆
    circles: [],
    // 控件 回到当前位置
    controls: [],
    //停车场详情dialog
    isParkDialogShow:false,
     //停车场详情dialog中的加载
     isParkDialogLoading:true,
    //当前选择的停车场
    selectedPark:{},
    //车辆信息列表
    parkCarsInfo:[]
  },
 
  // 回到当前位置
  locationClick: function (event) {
    var thisBlock = this;
 
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        
        thisBlock.setData({
          latitude: res.latitude,
          longitude: res.longitude,
 
        })
      },
    })
  },
 
  // 选择位置
  selectedClick: function() {
    // 设置权限
    wx.openSetting({
      success: function (res) {
        console.log(res);
 
        // 选择位置
        wx.chooseLocation({
          success: function (res) {
            console.log(res);
 
            // 打开位置
            wx.openLocation({
              latitude: res.latitude,
              longitude: res.longitude,
              name: res.name,
              address: res.address,
            })
          },
        })
      }      
    })    
  },
 
  regionChange: function(res) {
    // 改变中心点位置
    if (res.type == "end") {
      var thisBlock = this;
      this.mapCtx = wx.createMapContext("centerChange");
      this.mapCtx.getCenterLocation({
        success: function(res) {
          console.log(res);
 
          thisBlock.setData({
            latitude: res.latitude,
            longitude: res.longitude,
 
            markers: thisBlock.data.markers,
          })
        }
      })
    }
  },
 
  // 回到当前位置
  controlClick: function(res) {
    console.log(res);
    var thisBlock = this;
 
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        console.log(res);
 
        thisBlock.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers:thisBlock.data.markers,
        })
      },
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
    // 获取当前位置
    var thisBlock = this;
    wx.getLocation({
      type: "wgs84",
      success: function(res) {
        thisBlock.loadParks(res)

        console.log(res);
        thisBlock.data.markers.push( {iconPath: "/images/map/position.png",
        id:0,
        mType:'self',
        latitude: res.latitude,
        longitude: res.longitude,
        width:35,
        height:35,
        title:"",
      label: {content:"我的位置"},
      anchor:{}})
      thisBlock.reloadMap(res)
      },
    })
  },
 //刷新地图markers
  reloadMap:function(res){
    var thisBlock = this;
        thisBlock.setData({
          latitude: res.latitude,
          longitude: res.longitude,  
          //自身位置图标
          markers: thisBlock.data.markers ,
 
          circles: [{latitude: res.latitude,
            longitude: res.longitude,
            radius: 10,
            strokeWidth: 2,
            fillColor: "#FAFAD2",
            color: "#90EE90"}],
 
          controls: [{id:1001,
            position:{left:10, top:10, width:35, height:35},
            iconPath:"/images/map/returntomap.png",
            clickable:true}],
         
        })
        console.log('marker',this.data.markers)
  
  },
  //地图点击事件
onMapTap(e) {
  if(this.data.isParkDialogShow === true){
    this.setData({
      isParkDialogShow:false,
     isParkDialogLoading:true,
    })
  }
  },
//marker点击事件
  onMarkerTap(e) {
  
  let markerId = e.markerId
  let target = this.data.markers.find(it=> it.id === markerId)
  console.log("markertap", e.markerId,target)
  if(!target) return
  this.data.selectedPark = target
  if(target.mType === "park"){
    this.getCarsByPrakId(target.id)
    wx.showLoading({
      title: '加载车辆信息中',
    })
   
   
  }
  },
  //跳转至下订单页面
  doOrdering:function(e){
    wx.navigateTo({
      url: '../ordering/index?id='+1,
    })
    return
    console.log(e.currentTarget.dataset.id===undefined)
    if(e.currentTarget.dataset.id===undefined){
      wx.showToast({
        title: '未读取正确的车辆',
        icon:'error'
      })
    }else{
      wx.navigateTo({
        url: '../ordering/index?id='+e.currentTarget.dataset.id,
      })
    }
   
  },
  //设置停车场dialog visible
  setParkVisible:function(e){
    console.log(e)
    this.setData({
      isParkDialogShow:e.currentTarget.dataset.visible
    })
  },
  //根据停车场加载车辆信息
  getCarsByPrakId(id){
    wx.showLoading({
      title: '加载车辆中',
    })
    let that = this
    setTimeout(()=>{
      wx.hideLoading()
      that.setData({
        isParkDialogShow:true,
        scrollTop:0,
        parkCarsInfo : [{id:0,img:'',model:'比亚迪1A',price:'200',detail:'这是一段描述'},
        {id:1,img:'',model:'比亚迪1A',price:'200',detail:'这是一段描述'},
        {id:2,img:'',model:'比亚迪1A',price:'200',detail:'这是一段描述'},
        {id:3,img:'',model:'比亚迪1A',price:'200',detail:'这是一段描述'},
        {id:4,img:'',model:'比亚迪1A',price:'200',detail:'这是一段描述'},
        {id:5,img:'',model:'比亚迪1A',price:'200',detail:'这是一段描述'}]
      })
    },2000)
  },
//导航去当前选择的停车场
  guideToLocation:function(){
    if(!this.data.selectedPark?.id) {
      wx.showToast({title:'未选择停车场',icon:'error'})
      return
    }
    let target = this.data.selectedPark
    wx.openLocation({
      latitude: this.data.selectedPark.latitude,
      longitude: this.data.selectedPark.longitude,
    })
    console.log(this.data.selectedPark)
  },
  toRunningOrder(){
    wx.navigateTo({
      url: '/pages/order/running/index',
    })
  },
 //加载停车场列表
  loadParks:function(res){
   let data =  [{
    mType:'park',
    iconPath: "/images/map/park.png",
    id: Date.parse(new Date()),
    latitude: res.latitude+0.001,
    longitude: res.longitude+0.001,
    width: 35,
    height: 35,
    title: "当前位置",
    callout: {
      padding: 10,
      content:"可用车辆：5",
      bgColor:"#3f48cc",
      color:"#ffffff",
      display:"ALWAYS",
      borderRadius:5
    },
    label: {content:"标题"},
    anchor: {}
  },{
    iconPath: "/images/map/park.png",
    mType:'park',

    id: Date.parse(new Date())+1,
    latitude: 31.99100,
    longitude: 118.7700,
    width: 35,
    height: 35,
    title: "当前位置",
    callout: {
      padding: 10,
      content:"可用车辆：2",
      bgColor:"#3f48cc",
      color:"#ffffff",
      display:"ALWAYS",
      borderRadius:5
    },
    label: {content:"标题"},
    anchor: {}
  }]
  console.log('loading park')
  this.data.markers = this.data.markers.concat(data)
    
  },
  // 跳转到其他页面
  lineClick: function(res) {
    wx.navigateTo({
      url: '../map /mapline',
    })
  }
})